﻿using Application = System.Windows.Application;
using SMMCB = Sucrose.Memory.Manage.Constant.Backgroundog;
using SMMI = Sucrose.Manager.Manage.Internal;
using SMMRG = Sucrose.Memory.Manage.Readonly.General;
using SPMI = Sucrose.Pipe.Manage.Internal;
using SSDECT = Sucrose.Shared.Dependency.Enum.CommunicationType;
using SSDMMB = Sucrose.Shared.Dependency.Manage.Manager.Backgroundog;
using SSEHC = Sucrose.Shared.Engine.Helper.Compatible;
using SSEMI = Sucrose.Shared.Engine.Manage.Internal;
using SSEWVHM = Sucrose.Shared.Engine.WebView.Helper.Management;
using SSEWVMI = Sucrose.Shared.Engine.WebView.Manage.Internal;
using SSMI = Sucrose.Signal.Manage.Internal;
using SSPSBSS = Sucrose.Shared.Pipe.Services.BackgroundogPipeService;
using SSSHP = Sucrose.Shared.Space.Helper.Port;
using SSSSBSS = Sucrose.Shared.Signal.Services.BackgroundogSignalService;
using SSTSBTS = Sucrose.Shared.Transmission.Services.BackgroundogTransmissionService;
using SSWEW = Sucrose.Shared.Watchdog.Extension.Watch;
using STMI = Sucrose.Transmission.Manage.Internal;
using SWEACAM = Skylark.Wing.Extension.AudioController.AudioManager;
using SWEVPCAM = Skylark.Wing.Extension.VideoPlayerController.AudioManager;
using SWNM = Skylark.Wing.Native.Methods;

namespace Sucrose.Shared.Engine.WebView.Helper
{
    internal static class Web
    {
        public static async void Play()
        {
            try
            {
                if (!SSEWVMI.State)
                {
                    SSEWVMI.State = true;

                    //SSEWVMI.WebEngine.Source = SSEHS.GetSource(SSEWVMI.Web);

                    if (SSEMI.IntermediateD3DWindow > 0)
                    {
                        _ = SWNM.DebugActiveProcessStop((uint)SSEMI.IntermediateD3DWindow);
                    }
                }
            }
            catch (Exception Exception)
            {
                await SSWEW.Watch_CatchException(Exception);
            }
        }

        public static async void Pause()
        {
            try
            {
                if (SSEWVMI.State)
                {
                    SSEWVMI.State = false;

                    //string Path = SSEHS.GetImageContentPath();

                    //SSEHS.WriteImageContent(Path, await SSEWVES.Capture());

                    //SSEWVMI.WebEngine.Source = SSEHS.GetSource(Path);

                    if (SSEMI.IntermediateD3DWindow > 0)
                    {
                        _ = SWNM.DebugActiveProcess((uint)SSEMI.IntermediateD3DWindow);
                    }
                }
            }
            catch (Exception Exception)
            {
                await SSWEW.Watch_CatchException(Exception);
            }
        }

        public static async void StartCompatible()
        {
            try
            {
                if (SSEMI.Compatible.State)
                {
                    SMMI.BackgroundogSettingManager.SetSetting(SMMCB.AudioRequired, !string.IsNullOrEmpty(SSEMI.Compatible.SystemAudio));

                    switch (SSDMMB.CommunicationType)
                    {
                        case SSDECT.Pipe:
                            SMMI.BackgroundogSettingManager.SetSetting(SMMCB.PipeRequired, true);

                            _ = Task.Run(async () =>
                            {
                                SPMI.BackgroundogManager.MessageReceived += async (s, e) =>
                                {
                                    if (SSEWVMI.State)
                                    {
                                        SSPSBSS.Handler(e);

                                        await Application.Current.Dispatcher.InvokeAsync(async () =>
                                        {
                                            try
                                            {
                                                if (SSEWVMI.WebEngine.IsInitialized)
                                                {
                                                    SSEHC.ExecuteTask(SSEWVMI.WebEngine.CoreWebView2.ExecuteScriptAsync);
                                                }
                                            }
                                            catch (Exception Exception)
                                            {
                                                await SSWEW.Watch_CatchException(Exception);
                                            }
                                        });
                                    }
                                };

                                await SPMI.BackgroundogManager.StartServer();
                            });
                            break;
                        case SSDECT.Signal:
                            SMMI.BackgroundogSettingManager.SetSetting(SMMCB.SignalRequired, true);

                            SSMI.BackgroundogManager.StartChannel(async (s, e) =>
                            {
                                if (SSEWVMI.State)
                                {
                                    SSSSBSS.Handler(s, e);

                                    await Application.Current.Dispatcher.InvokeAsync(async () =>
                                    {
                                        try
                                        {
                                            if (SSEWVMI.WebEngine.IsInitialized)
                                            {
                                                SSEHC.ExecuteTask(SSEWVMI.WebEngine.CoreWebView2.ExecuteScriptAsync);
                                            }
                                        }
                                        catch (Exception Exception)
                                        {
                                            await SSWEW.Watch_CatchException(Exception);
                                        }
                                    });
                                }
                            });
                            break;
                        case SSDECT.Transmission:
                            STMI.BackgroundogManager = new(SMMRG.Loopback, SSSHP.Available(SMMRG.Loopback));

                            SMMI.BackgroundogSettingManager.SetSetting(SMMCB.TransmissionRequired, true);
                            SMMI.BackgroundogSettingManager.SetSetting(SMMCB.TransmissionPort, STMI.BackgroundogManager.Port);

                            _ = Task.Run(async () =>
                            {
                                STMI.BackgroundogManager.MessageReceived += async (s, e) =>
                                {
                                    if (SSEWVMI.State)
                                    {
                                        SSTSBTS.Handler(e);

                                        await Application.Current.Dispatcher.InvokeAsync(async () =>
                                        {
                                            try
                                            {
                                                if (SSEWVMI.WebEngine.IsInitialized)
                                                {
                                                    SSEHC.ExecuteTask(SSEWVMI.WebEngine.CoreWebView2.ExecuteScriptAsync);
                                                }
                                            }
                                            catch (Exception Exception)
                                            {
                                                await SSWEW.Watch_CatchException(Exception);
                                            }
                                        });
                                    }
                                };

                                await STMI.BackgroundogManager.StartServer();
                            });
                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception Exception)
            {
                await SSWEW.Watch_CatchException(Exception);
            }
        }

        public static async void SetVolume(int Volume)
        {
            try
            {
                if (SSEMI.Processes.Any())
                {
                    foreach (int Process in SSEMI.Processes.ToList())
                    {
                        try
                        {
                            SWEVPCAM.SetApplicationVolume(Process, Volume);
                        }
                        catch
                        {
                            try
                            {
                                SWEACAM.SetApplicationVolume(Process, Volume);
                            }
                            catch { }
                        }
                    }
                }

                if (SSEWVMI.Try < 3)
                {
                    await Task.Run(() =>
                    {
                        SSEWVMI.Try++;
                        SSEWVHM.SetProcesses();
                    });
                }
            }
            catch (Exception Exception)
            {
                await SSWEW.Watch_CatchException(Exception);
            }
        }
    }
}